package com.project.dao.Impl;

import com.project.bean.RoomBean;
import com.project.bean.RoomFacilityBean;
import com.project.dao.IRoomFacilityDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Repository
public class RoomFacilityDaoImpl extends BaseDao implements IRoomFacilityDao {
    @Override
    public void delByRoom(int roomId) {
        this.setConnection();
        try {
            ps=con.prepareStatement("delete from s_roomState where fk_roomId=?");
            ps.setObject(1,roomId);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public List<RoomFacilityBean> findByRoomId(int roomId) {
        List<RoomFacilityBean> list=new ArrayList<>();
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT s.* FROM s_roomState s JOIN r_room r ON s.fk_roomId=r.pk_roomId WHERE r.pk_roomId= ?");
            ps.setObject(1,roomId);
            rs=ps.executeQuery();
            while (rs.next()){
                list.add(this.getFacBean());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return list;
    }

    private RoomFacilityBean getFacBean() {
        RoomFacilityBean fac = new RoomFacilityBean();
        try {
            fac.setId(rs.getInt("pk_id"));
            fac.setDescribe(rs.getString("s_describe"));
            fac.setState(rs.getString("s_state"));
            fac.setDate(LocalDate.parse(rs.getString("s_date")));
            RoomBean roomBean = new RoomBean();
            roomBean.setId(rs.getInt("fk_roomId"));
            fac.setRoomBean(roomBean);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return fac;
    }

    @Override
    public void addFacility(RoomFacilityBean roomFacility) {
        this.setConnection();
        try {
            ps=con.prepareStatement("INSERT INTO s_roomState(s_describe,s_date,s_state,fk_roomId)\n" +
                    "VALUE(?,?,?,?)");
            ps.setObject(1,roomFacility.getDescribe());
            ps.setObject(2,roomFacility.getDate());
            ps.setObject(3,roomFacility.getState());
            ps.setObject(4,roomFacility.getRoomBean().getId());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void updateStatus(Integer facilityId, String setStatus) {
        this.setConnection();
        try {
            ps=con.prepareStatement("update s_roomState set s_state=? where pk_id=?");
            ps.setObject(1,setStatus);
            ps.setObject(2,facilityId);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public int findRoomFacilityNum(Integer roomId, String setStatus) {
        int sum=0;
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT  COUNT(s.pk_id) SUM FROM s_roomState s JOIN r_room r ON s.fk_roomId=r.pk_roomId WHERE r.pk_roomId=? AND s_state=?\n" +
                    "\n");
            ps .setObject(1,roomId);
            ps.setObject(2,setStatus);
            rs=ps.executeQuery();
            while (rs.next()){
                sum=rs.getInt("sum");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return sum;
    }

}
